//获取地址栏传来的参数
var flag = 0;

var response;
var namelist_git = []; //员工名单(gitname形式)
var namelist_cn = []; //员工名单(中文名形式)
var total_add_lines = [];
var total_del_lines = [];
var total_lines = [];
var total_commits = [];

function getDate() {
    var time = new Date();
    document.getElementById("start_time").value = addDate(time, -30);
    document.getElementById("end_time").value = addDate(time, +1);
    AddDepartment();
    search();
}

//向页面添加端别信息
function AddDepartment() {
    var items;
    var item1 = "<option selected='selected' value='web'>Web端</option>";
    var item2 = "<option value='server'>Server端</option>";
    items = item1 + item2;
    $('#department_select').html(items);
}

//通过gitname得到中文名的列表
function GetChineseNameList() {
    $.ajax({
        url: "/staff/getAllRelations",
        data: {},
        type: "Post",
        async: false,
        success: function (data) {
            var temp = JSON.stringify(data);
            var tempJson = JSON.parse(temp);
            namelist_cn = [];
            for (var i = 0; i < namelist_git.length; i++) {
                for (var j = 0; j < tempJson.length; j++) {
                    //通过关系表找到对应的汉语名
                    if (tempJson[j].gitName === namelist_git[i]) {
                        namelist_cn.push(tempJson[j].chineseName)
                    }
                }
            }
            console.log(namelist_cn);
        }
    });
}

function search() {
    flag = 1;
    document.getElementById("highchart").style.display = "block";
    document.getElementById("box_center").style.display = "block";
    document.getElementById("table").style.display = "block";
    var department = document.getElementById("department_select").value;
    var start = document.getElementById("start_time").value;
    var end = document.getElementById("end_time").value;

    //得到某端别的所有员工
    $.ajax({
        url: "/staff/getAllStaff",
        data: {
            department: department
        },
        type: "Post",
        async: false,
        success: function (data) {
            var temp = JSON.stringify(data);
            var tempJson = JSON.parse(temp);
            namelist_git = [];
            for (var i = 0; i < tempJson.length; i++) {
                //namelist_git是一个元素为String的数组，代表每个人的git名
                namelist_git.push(tempJson[i].gitname);
            }
            console.log(namelist_git);
            GetChineseNameList();
        }
    });
    //取得每人每个仓库的数据
    $.ajax({
        url: "/codestat/only_master/getStatsByDepartmentByTime",
        data: {
            start: start,
            end: end,
            department: department
        },
        type: "Post",
        async: false,
        success: function (data) {
            var temp = JSON.stringify(data);
            response = JSON.parse(temp);
            console.log("response", response);
            //初始化
            total_add_lines = [];
            total_del_lines = [];
            total_lines = [];
            total_commits = [];
            for (var i = 0; i < namelist_git.length; i++) {
                var add_num = 0;
                var del_num = 0;
                var total_num = 0;
                var commit_num = 0;
                for (var j = 0; j < response.length; j++) {
                    if (response[j].rd_name === namelist_git[i]) {
                        add_num += response[j].total_add_line;
                        del_num += response[j].total_del_line;
                        total_num += response[j].total_line;
                        commit_num += response[j].total_commits;
                    }
                }
                total_add_lines.push(add_num);
                total_del_lines.push(del_num);
                total_lines.push(total_num);
                total_commits.push(commit_num);
            }
            console.log("total_add_lines", total_add_lines);
            console.log("total_del_lines", total_del_lines);
            console.log("total_lines", total_lines);
            console.log("total_commits", total_commits);
            $('#show_table').bootstrapTable('destroy');
            $('#show_table').bootstrapTable({
                url: "/codestat/only_master/getStatsByDepartmentByTime",
                type: "post",
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                queryParams: {
                    start: start,
                    end: end,
                    department: department
                },
                cache: false,
                async: false,
                striped: true,
                pagination: true,
                sortable: true,
                sortOrder: "desc",//降序
                sortName: 'rd_name',
                pageSize: 10,
                pageNumber: 1,
                pageList: [10, 20, 50, 100, 200, 500],
                search: true,
                strictSearch: false,
                showColumns: true,
                showRefresh: false,
                showExport: false,
                columns: [
                    {
                        field: "rd_name",
                        title: "姓名",
                        align: "center",
                        valign: "middle",
                        sortable: "true",
                        formatter: function (data) {
                            var index = namelist_git.indexOf(data);
                            return namelist_cn[index];
                        }
                    },
                    {
                        field: "function_name",
                        title: "仓库",
                        align: "center",
                        valign: "middle",
                        sortable: "false"
                    },
                    {
                        field: "total_add_line",
                        title: "增加行数",
                        align: "center",
                        valign: "middle",
                        sortable: "false"
                    },
                    {
                        field: "total_del_line",
                        title: "删除行数",
                        align: "center",
                        valign: "middle",
                        sortable: "false"
                    },
                    {
                        field: "total_line",
                        title: "总行数",
                        align: "center",
                        valign: "middle",
                        sortable: "false"
                    },
                    {
                        field: "total_commits",
                        title: "总提交数",
                        align: "center",
                        valign: "middle",
                        sortable: "false"
                    },
                    {
                        field: "branch",
                        title: "分支",
                        align: "center",
                        valign: "middle",
                        sortable: "false"
                    },
                ]
            });
        }
    });
    Highcharts.chart('highchart', {
        title: {
            text: 'Git代码上传量等指标统计情况'
        },
        xAxis: {
            categories: namelist_cn
        },
        yAxis: {
            title: {
                text: '数量(行数)'
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle'
        },
        tooltip: {
            split: false,
            shared: true,
            headerFormat: '<span style="font-size:11px">{point.x}</span><br>',
            pointFormat: '<span style="color:{point.color}">{series.name}</span>: <b>{point.y}</b><br>'
        },
        series: [
            {
                name: '增加代码行数',
                data: total_add_lines
            }, {
                name: '删除代码行数',
                data: total_del_lines
            }, {
                name: '代码总行数',
                data: total_lines
            }, {
                name: '总提交数量',
                data: total_commits
            }],
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 500
                },
                chartOptions: {
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom'
                    }
                }
            }]
        }
    });
}